﻿using AutoMapper;
using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Write.API.Command.LeaveCommand;
using MediatR;

namespace B.S.BaseData.Write.API.CommandHandler.LeaveHandler
{
    public class ApprovalLeaveHandler : IRequestHandler<ApprovalLeaveCommand, APIResult<int>>
    {
        private readonly IBaseRepository<Leave> leaveRep;
        private readonly IMapper mapper;
        private readonly IBaseRepository<Record> reRep;

        public ApprovalLeaveHandler(IBaseRepository<Leave> leaveRep,IMapper mapper,IBaseRepository<Record> reRep)
        {
            this.leaveRep = leaveRep;
            this.mapper = mapper;
            this.reRep = reRep;
        }
        public Task<APIResult<int>> Handle(ApprovalLeaveCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            //查询当前申请表
            var leave = leaveRep.GetAll().FirstOrDefault(x => x.Id == request.Id);
            //查询领导人的人数 LeaderCount
            var LeaderCount = leave.LeaderIds.Split(',').Length;
            //查询已审批人数 AppCount
            var AppCount = reRep.GetAll().Where(x => x.LeaveId == request.Id).ToList().Count;

            //无人审批 AppCount==0||AppCount<LeaderCount-1 通过 审批中 不通过 审批中  
            if (AppCount == 0 || AppCount < LeaderCount - 1)
            {

                var m = mapper.Map<Leave>(leave);
                m.ApprovalStatus = (int)ApprovalStatus.审核中;
                m.ApprovalStatusName = "审核中";
                leaveRep.Update(m);

            }

            //最后一人审批 通过 已审批 不通过 已拒绝

            if (AppCount == LeaderCount - 1)
            {
                if (request.AppResult == (int)AppResult.同意)
                {
                    var m = mapper.Map<Leave>(leave);
                    m.ApprovalStatus = (int)ApprovalStatus.已审核;
                    m.ApprovalStatusName = "已审核";
                    leaveRep.Update(m);

                }
                else
                {
                    var m = mapper.Map<Leave>(leave);
                    m.ApprovalStatus = (int)ApprovalStatus.已拒绝;
                    m.ApprovalStatusName = "已拒绝";
                    leaveRep.Update(m);

                }

            }

            //所有人审批且通过 通过已通过 不通过已拒绝
            var AppCounts = reRep.GetAll().Where(x => x.LeaveId == request.Id && x.AppResult == (int)AppResult.同意).ToList().Count;
            if (AppCount == LeaderCount && AppCounts == LeaderCount)
            {
                if (request.AppResult == (int)AppResult.同意)
                {
                    var m = mapper.Map<Leave>(leave);
                    m.ApprovalStatus = (int)ApprovalStatus.终审通过;
                    m.ApprovalStatusName = "终审通过 ";
                    leaveRep.Update(m);

                }
                else
                {
                    var m = mapper.Map<Leave>(leave);
                    m.ApprovalStatus = (int)ApprovalStatus.终审驳回;
                    m.ApprovalStatusName = "终审驳回";
                    leaveRep.Update(m);

                }

            }

            var record = new Record();
                //记录表的添加
                record.LeaveId = request.Id;
                record.OperatorId = request.UserId;
                record.OperatorName=request.Name;
                if (request.AppResult == (int)AppResult.同意)
                {
                    record.AppResult = request.AppResult;
                    record.Reason = "";
                }
                else
                {
                    record.AppResult = request.AppResult;
                    record.Reason = request.Reason;
                }
               
               
                record.CreateTime = DateTime.Now;
                
                reRep.Add(record);
;               result.Code = APIEnum.Success;
                result.Msg = "审批成功";
                result.Data = 1;
          

           
            return Task.FromResult(result);

        }
    }
}
